FROM bishengjdk:1.8.262.centos7

ENV CATALINA_HOME /usr/local/tomcat
ENV PATH $CATALINA_HOME/bin:$PATH
RUN mkdir -p "$CATALINA_HOME"
WORKDIR $CATALINA_HOME

# let "Tomcat Native" live somewhere isolated
ENV TOMCAT_NATIVE_LIBDIR $CATALINA_HOME/native-jni-lib
ENV LD_LIBRARY_PATH ${LD_LIBRARY_PATH:+$LD_LIBRARY_PATH:}$TOMCAT_NATIVE_LIBDIR

# see https://www.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/KEYS
# see also "update.sh" (https://github.com/docker-library/tomcat/blob/master/update.sh)
ENV GPG_KEYS 05AB33110949707C93A279E3D3EFE6B686867BA6 07E48665A34DCAFAE522E5E6266191C37C037D42 47309207D818FFD8DCD3F83F1931D684307A10A5 541FBE7D8F78B25E055DDEE13C370389288584E7 61B832AC2F1C5A90F0F9B00A1C506407564C17A3 713DA88BE50911535FE716F5208B0AB1D63011C7 79F7026C690BAA50B92CD8B66A3AD3F4F22C4FED 9BA44C2621385CB966EBA586F72C284D731FABEE A27677289986DB50844682F8ACB77FC2E86E29AC A9C5DF4D22E99998D9875A5110C01C5A2F6059E7 DCFD35E0BF8CA7344752DE8B6FB21E8933C60243 F3A04C595DB5B6A5F1ECA43E3B7BBB100D811BBE F7DA48BB64BCB84ECBA7EE6935CD23C10D498E23

ENV TOMCAT_MAJOR 8
ENV TOMCAT_VERSION 8.5.61
ENV TOMCAT_SHA512 55d7d0442a3c1bae7e470ba75c7473a8deeaf33c60ff66cecdc136ae0b0e852b871940c5deb720d501b444ccb00f0b8520b1930cbf564c2f46ceaf1e0367d41b

# # 下载
# # https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394
# ENV TOMCAT_TGZ_URL https://www.apache.org/dyn/closer.cgi?action=download&filename=tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz
# # not all the mirrors actually carry the .asc files :'(
# ENV TOMCAT_ASC_URL http://archive.apache.org/dist/tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc

RUN set -eux; \
	\
    yum -y update; yum upgrade -y; \
    yum install -y ca-certificates openssl; \
	yum install -y gnupg wget; \
	yum clean all;
RUN set -eux; \
	ddist() { \
		local f="$1"; shift; \
		local distFile="$1"; shift; \
		local mvnFile="${1:-}"; \
		local success=; \
		local distUrl=; \
		for distUrl in \
# zh
			"https://mirror.bit.edu.cn/apache/$distFile" \
			# "https://downloads.apache.org/$distFile" \
# https://issues.apache.org/jira/browse/INFRA-8753?focusedCommentId=14735394#comment-14735394
			"https://www.apache.org/dyn/closer.cgi?action=download&filename=$distFile" \
# if the version is outdated (or we're grabbing the .asc file), we might have to pull from the dist/archive :/
			"https://www-us.apache.org/dist/$distFile" \
			"https://www.apache.org/dist/$distFile" \
			"https://archive.apache.org/dist/$distFile" \
# if all else fails, let's try Maven (https://www.mail-archive.com/users@tomcat.apache.org/msg134940.html; https://mvnrepository.com/artifact/org.apache.tomcat/tomcat; https://repo1.maven.org/maven2/org/apache/tomcat/tomcat/)
			${mvnFile:+"https://repo1.maven.org/maven2/org/apache/tomcat/tomcat/$mvnFile"} \
		; do \
			if wget -O "$f" "$distUrl" && [ -s "$f" ]; then \
				success=1; \
				break; \
			fi; \
		done; \
		[ -n "$success" ]; \
	}; \
	\
	ddist 'tomcat.tar.gz' "tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz" "$TOMCAT_VERSION/tomcat-$TOMCAT_VERSION.tar.gz"; \
	echo "$TOMCAT_SHA512 *tomcat.tar.gz" | sha512sum -c -; \
	ddist 'tomcat.tar.gz.asc' "tomcat/tomcat-$TOMCAT_MAJOR/v$TOMCAT_VERSION/bin/apache-tomcat-$TOMCAT_VERSION.tar.gz.asc" "$TOMCAT_VERSION/tomcat-$TOMCAT_VERSION.tar.gz.asc"; \
	# RUN -eux; \
    # for key in $GPG_KEYS; do \
	# 	gpg --keyserver ha.pool.sks-keyservers.net --keyserver-options timeout=20 --recv-keys "$key"; \
	# done; \
	# gpg --batch --verify tomcat.tar.gz.asc tomcat.tar.gz; \
	tar -xf tomcat.tar.gz --strip-components=1; \
	rm bin/*.bat; \
	rm tomcat.tar.gz; \
	command -v gpgconf && gpgconf --kill all || :; \
	\
    # https://tomcat.apache.org/tomcat-9.0-doc/security-howto.html#Default_web_applications
	rm -rf webapps; \
	mkdir webapps;

	# nativeBuildDir="$(mktemp -d)"; \
	# tar -xf bin/tomcat-native.tar.gz -C "$nativeBuildDir" --strip-components=1; \
	# apt-get install -y --no-install-recommends --virtual .native-build-deps \
    #     apr-dev \
    #     dpkg-dev dpkg \
    #     gcc \
    #     libc-dev \
    #     make \
    #     openssl-dev \
	# ; \
	# ( \
	# 	export CATALINA_HOME="$PWD"; \
	# 	cd "$nativeBuildDir/native"; \
	# 	gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
	# 	aprConfig="$(command -v apr-1-config)"; \
	# 	./configure \
	# 		--build="$gnuArch" \
	# 		--libdir="$TOMCAT_NATIVE_LIBDIR" \
	# 		--prefix="$CATALINA_HOME" \
	# 		--with-apr="$aprConfig" \
	# 		--with-java-home="$JAVA_HOME" \
	# 		--with-ssl=yes; \
	# 	make -j "$(nproc)"; \
	# 	make install; \
	# ); \
    # runDeps="$( \
    #             scanelf --needed --nobanner --recursive "$TOMCAT_NATIVE_LIBDIR" \
    #                     | awk '{ gsub(/,/, "\nso:", $2); print "so:" $2 }' \
    #                     | sort -u \
    #                     | xargs -r apt-get info --installed \
    #                     | sort -u \
    #     )" \
    # apt-get install -y --virtual .tomcat-native-rundeps $runDeps; \
    # apt-get del .fetch-deps .native-build-deps; \
	# rm -rf "$nativeBuildDir"; \
	# rm bin/tomcat-native.tar.gz; \
	# \
	RUN set -eux; \
# sh removes env vars it doesn't support (ones with periods)
# https://github.com/docker-library/tomcat/issues/77
	find ./bin/ -name '*.sh' -exec sed -ri 's|^#!/bin/sh$|#!/usr/bin/env bash|' '{}' +; \
	\
# fix permissions (especially for running as non-root)
# https://github.com/docker-library/tomcat/issues/35
	chmod -R +rX .; \
	chmod 777 logs temp work; \
	\
# smoke test
	catalina.sh version

EXPOSE 8080
CMD ["catalina.sh", "run"]
